<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;

use app\admin\controller\User;
use \think\Db;
use \think\Cookie;
class Index extends User
{
    public function index()
    {
        $menu = Db::name('admin_menu')->where(['is_display'=>1])->order('orders asc')->select();
        //添加url
        foreach ($menu as $key => $value) {
        	if(empty($value['parameter'])) {
        		$url = url($value['module'].'/'.$value['controller'].'/'.$value['function']);
        	} else {
                $url = url($value['module'].'/'.$value['controller'].'/'.$value['function'].','.$value['parameter']);
        	}
        	$menu[$key]['url'] = $url;
        }
        $menus = $this->menulist($menu);
        $this->assign('menus',$menus);

        return $this->fetch();
    }


    protected function menulist($menu){
		$menus = array();
		//先找出顶级菜单
		foreach ($menu as $k => $val) {
			if($val['pid'] == 0) {
				$menus[$k] = $val;
			}
		}
		//通过顶级菜单找到下属的子菜单
		foreach ($menus as $k => $val) {
			foreach ($menu as $key => $value) {
				if($value['pid'] == $val['id']) {
					$menus[$k]['list'][] = $value;
				}
			}
		}
		return $menus;
	}


	//配置首页
	public function setIndex(){
        $setData = db("indexset")->select();
        foreach ($setData as $key=>$value) {
            if($value['id']==3){//推荐攻略
                $article_str_arr = explode(",",$setData[$key]['id_str']);
                $article_arr = [];
                foreach ($article_str_arr as $k=>$v) {
                    $article_id = explode("_",$v);
                    $article_arr[] = ['num'=>$article_id[0],'id'=>$article_id[1]];
                }
                $articleNames = '<br>';
                foreach ($article_arr as $k=>$v) {
                    $articleData = db("article")->where("id",$v['id'])->field("title")->find();
                    $articleNames .= "【".$v['num']."】- ".$articleData['title']."<br><br>";
                }
                $setData[$key]['id_data'] = $articleNames;
            }else{
                $where["goods_id"] = ["in",$setData[$key]['id_str']];
                $Goods = db("goods")
                    ->where($where)
                    ->field("goods_id,goods_name")
                    ->select();
                $goodsNames = '<br>';
                foreach ($Goods as $k=>$v) {
                    $goodsNames .= "【".$v['goods_id']."】- ".$v['goods_name']."<br><br>";
                }
                $setData[$key]['id_data'] = $goodsNames;
            }
        }

        $this->assign('setData', $setData);
        return $this->fetch();
    }

    //当季活动配置页
	public function setSeason(){
        if($this->request->isPost()) {
            $post = $this->request->post();
            if(!empty($post['idstr'])){
                $idstr = $post['idstr'];
                $id_arr = explode(",",$idstr);
                foreach ($id_arr as $v) {
                    $id_goods = db("goods")->where("goods_id",$v)->find();
                    if(empty($id_goods)){
                        return $this->error('请传入正确商品ID');
                    }
                }

                if(false == Db::name('indexset')->where('id',1)->update(["id_str"=>$idstr])) {
                    return $this->error('修改失败');
                } else {
                    return $this->success('修改成功');
                }
            }else{
                return $this->error('无数据传入');
            }
        }else{
            $seasonData = db("indexset")->where("id",1)->find();
            if(!empty($seasonData['id_str'])){
                $id_str = $seasonData['id_str'];
                $id_arr = explode(",",$id_str);
            }else{
                $id_arr = [0=>'',1=>'',2=>''];
            }

            $this->assign('id_arr', $id_arr);
            return $this->fetch();
        }
    }

    //热门活动配置页
	public function setHot(){
        if($this->request->isPost()) {
            $post = $this->request->post();
            if(!empty($post['idstr'])){
                $idstr = $post['idstr'];
                $id_arr = explode(",",$idstr);
                foreach ($id_arr as $v) {
                    $id_goods = db("goods")->where("goods_id",$v)->find();
                    if(empty($id_goods)){
                        return $this->error('请传入正确商品ID');
                    }
                }

                if(false == Db::name('indexset')->where('id',2)->update(["id_str"=>$idstr])) {
                    return $this->error('修改失败');
                } else {
                    return $this->success('修改成功');
                }
            }else{
                return $this->error('无数据传入');
            }
        }else{
            $seasonData = db("indexset")->where("id",2)->find();
            if(!empty($seasonData['id_str'])){
                $id_str = $seasonData['id_str'];
                $id_arr = explode(",",$id_str);
            }else{
                $id_arr = [0=>'',1=>'',2=>''];
            }

            $this->assign('id_arr', $id_arr);
            return $this->fetch();
        }
    }

    //推荐攻略配置页
	public function setArticle(){
        if($this->request->isPost()) {
            $post = $this->request->post();
            if(!empty($post['idstr'])){
                $idstr = $post['idstr'];
                if(false == Db::name('indexset')->where('id',3)->update(["id_str"=>$idstr])) {
                    return $this->error('修改失败');
                } else {
                    return $this->success('修改成功');
                }
            }else{
                return $this->error('无数据传入');
            }
        }else{
            $articleData = db("indexset")->where("id",3)->find();
            if(!empty($articleData['id_str'])){
                $id_str = $articleData['id_str'];
                $id_arr = explode(",",$id_str);

                $final_id_arr = [];
                for ($i=0;$i<20;$i++){
                    $final_id_arr[$i] = '';
                }
                foreach ($id_arr as $k=>$v) {
                    $arr = explode("_",$v);
                    $show_key = $arr[0]-1;
                    $final_id_arr[$show_key] = $arr[1];
                }
            }else{
                $final_id_arr = [];
                for ($i=0;$i<20;$i++){
                    $final_id_arr[$i] = '';
                }
            }

            $article = db("article")->where('status',1)->field("id,title")->order("create_time desc")->select();
            $this->assign('article',$article);
            $this->assign('id_arr', $final_id_arr);
            return $this->fetch();
        }
    }




}
